package Servlet;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyBaseServlet extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
//        获取传递的方法参数值，以便在执行中通过反射动态调用对应的业务逻辑
        String method = servletRequest.getParameter("method");
        try {
            Method method1 = this.getClass().getMethod(method, ServletRequest.class, ServletResponse.class);
//        执行中动态的调用对应的方法
            method1.invoke(this,servletRequest,servletResponse);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
